package com.cxb;

import java.util.Hashtable;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.cxb.manager.PhoneManager;
import com.cxb.manager.PhoneSignalInfo;
import com.cxb.utils.SharedPreferencesUtils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

public class RechangeActivity extends BaseActivity {
	TextView tvNumber, tvOperator, tvTip;
	SharedPreferencesUtils sp;
	View bt_back;

	private static int QR_SIDE = 190;
	private ImageView iv_qrcode;

	private void createImage(String text) {
		try {
			if (text == null || "".equals(text) || text.length() < 1) {
				return;
			}
			Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
			hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
			BitMatrix bitMatrix = new QRCodeWriter().encode(text,
					BarcodeFormat.QR_CODE, QR_SIDE, QR_SIDE, hints);
			int[] pixels = new int[QR_SIDE * QR_SIDE];
			for (int y = 0; y < QR_SIDE; y++) {
				for (int x = 0; x < QR_SIDE; x++) {
					if (bitMatrix.get(x, y)) {
						pixels[y * QR_SIDE + x] = 0xff000000;
					} else {
						pixels[y * QR_SIDE + x] = 0xffffffff;
					}

				}
			}

			Bitmap bitmap = Bitmap.createBitmap(QR_SIDE, QR_SIDE,
					Bitmap.Config.ARGB_8888);

			bitmap.setPixels(pixels, 0, QR_SIDE, 0, 0, QR_SIDE, QR_SIDE);
			iv_qrcode.setImageBitmap(bitmap);

		} catch (WriterException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting_rechange);
		sp = new SharedPreferencesUtils(RechangeActivity.this);
		iv_qrcode = (ImageView) findViewById(R.id.iv_qrcode);
		
//		createImage("http://jx10010.cn/mobileReq!index.ac?phonenumber="+sp.getLocalNumber());
		createImage("http://wap.10010.com/t/siteMap.htm?menuId=pay");
		init();
		
	}

	private void init() {

		tvNumber = (TextView) findViewById(R.id.tv_recharge_number);
		tvOperator = (TextView) findViewById(R.id.tv_recharge_operator_name);
		tvTip = (TextView) findViewById(R.id.tv_recharge_tip);
		bt_back = findViewById(R.id.bt_back);
		bt_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		tvNumber.setText(getString(R.string.setting_location_number)
				+ sp.getLocalNumber());
		switch (PhoneSignalInfo.getMobileOperator(getApplicationContext())) {
		case 0:
			tvOperator
					.setText(getString(R.string.setting_recharge_carrier_name)
							+ "");
			break;
		case 1:
			tvOperator
					.setText(getString(R.string.setting_recharge_carrier_name)
							+ getString(R.string.carrier_cmcc));
			break;
		case 2:
			tvOperator
					.setText(getString(R.string.setting_recharge_carrier_name)
							+ getString(R.string.carrier_unicom));
			break;
		case 3:
			tvOperator
					.setText(getString(R.string.setting_recharge_carrier_name)
							+ getString(R.string.carrier_telecom));
			break;
		case 4:
			tvOperator
					.setText(getString(R.string.setting_recharge_carrier_name)
							+ getString(R.string.carrier_other));
			break;
		default:
			break;
		}

	}
}
